package com.community.process._1010.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.process._1010.common.api.ErrorAssert;
import com.community.process._1010.common.utils.PageUtil;
import com.community.process._1010.common.utils.mp.MpPageFormat;
import com.community.process._1010.dao.entity.ProcessIngredients;
import com.community.process._1010.dao.mapper.ProcessIngredientsMapper;
import com.community.process._1010.dao.service.ProcessCategoryIService;
import com.community.process._1010.dao.view.ProcessIngredientsView;
import com.community.process._1010.dao.vo.ProcessIngredientsVo;
import com.community.process._1010.domains.ProcessIngredientsDTO;
import com.community.process._1010.service.ProcessIngredientsService;
import com.oneadds.lowcode.BaseServiceImpl;
import com.oneadds.lowcode.dto.PageListData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName ProcessIngredientsServiceImpl
 * @Description TODO
 * @Author jamesj
 * @Date 2024/9/5 16:55
 */
@Service
@RequiredArgsConstructor
public class ProcessIngredientsServiceImpl extends BaseServiceImpl<ProcessIngredientsMapper, ProcessIngredients> implements ProcessIngredientsService {

    private final ProcessCategoryIService processCategoryIService;

    @Override
    public ProcessIngredientsView getIngredientById(Long id) {
        ProcessIngredients ingredients = this.getById(id);
        ErrorAssert.notNull(ingredients, "食材不存在");
        return new ProcessIngredientsView(ingredients,processCategoryIService.findParentIdsByChildId(ingredients.getCategoryId()));
    }

    @Override
    public PageListData<ProcessIngredientsVo> getIngredientPageList(ProcessIngredientsDTO.IngredientsPageSearch dto) {
        // yg_process_ingredients ypi 食材
        // yg_process_category ypc 分类
        QueryWrapper<ProcessIngredientsVo> wrapper = new QueryWrapper<>();
        wrapper.eq(dto.getCategoryId() != null,"ypi.category_id", dto.getCategoryId());
        wrapper.eq(dto.getUp() != null,"ypi.up", dto.getUp());
        wrapper.like(StrUtil.isNotBlank(dto.getName()),"ypi.`name`", dto.getName());
        wrapper.likeRight(StrUtil.isNotBlank(dto.getCategoryTreeCode()),"ypc.`tree_code`", dto.getCategoryTreeCode());
        wrapper.eq("ypi.deleted", 0);
        wrapper.eq("ypc.deleted", 0);
        wrapper.orderByDesc("ypi.`up`","ypi.`create_time`");
        Page<ProcessIngredientsVo> page = this.baseMapper.getProcessIngredientsVo(PageUtil.page(dto), wrapper);
        return MpPageFormat.format(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addIngredients(ProcessIngredientsDTO.IngredientsAdd dto) {
        this.save(new ProcessIngredients(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIngredients(ProcessIngredientsDTO.IngredientsUpdate dto) {
        this.updateById(new ProcessIngredients(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteIngredients(ProcessIngredientsDTO.IngredientsDelete dto) {
        this.removeByIds(dto.getIds());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setUp(ProcessIngredientsDTO.IngredientsSetUp dto) {
        this.lambdaUpdate().in(ProcessIngredients::getId, dto.getIds())
                .set(ProcessIngredients::getUp, dto.getUp())
                .update();
    }
}
